<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis;

use app\enum\AnalysisEnum;
use app\enum\CommonEnum;
use app\model\mongo\MongoReportModel;
use app\service\BaseService;

/**
 * 数据基础服务.
 */
final class AnalysisService extends BaseService
{
    /**
     * 获取数据表模型.
     * @param int $mediaId 媒体id
     * @param string $tableType 表类型
     */
    public static function getReportModel(int $mediaId = 0, string $tableType = ''): MongoReportModel
    {
        $tableName = self::getTablePrefix($mediaId);
        $classname = '\app\model\mongo\Mongo' . ucfirst($tableName) . ucfirst($tableType) . 'DayReportModel';
        return new $classname();
    }

    /**
     * 获取信息表模型.
     * @param int $mediaId 媒体id
     * @param string $tableType 表类型
     */
    public static function getInfoModel(int $mediaId = 0, string $tableType = '')
    {
        $tableName = self::getTablePrefix($mediaId);
        if ($mediaId == CommonEnum::KS_MEDIA && $tableType == AnalysisEnum::TYPE_VIDEO) {
            $classname = '\app\model\mongo\MongoKsUserVideoModel';
        } else {
            $classname = '\app\model\mongo\Mongo' . ucfirst($tableName) . ucfirst($tableType) . 'Model';
        }
        return new $classname();
    }

    /**
     * 获取汇总模型.
     *
     * @param int $mediaId 媒体id
     * @param int $taskId 任务id
     * @return mixed
     */
    public static function getStatModelList(int $mediaId = 0, string $dataType = '', int $taskId = 0)
    {
        $classname = AnalysisEnum::STAT_MODEL_LIST[$mediaId][$dataType][$taskId];
        return new $classname();
    }

    /**
     * 获取服务
     *
     * @param int $mediaId 媒体id
     * @param string $serviceName 服务类型
     * @param string $dataType 数据类型
     * @return null|mixed
     */
    public static function getService(int $mediaId = 0, string $serviceName = '', string $dataType = '')
    {
        $classname = '';
        if ($mediaId == CommonEnum::KS_MEDIA) {
            $classname = '\app\service\analysis\\' . $serviceName . '\kuaishou\\' . ucfirst($dataType) . ucfirst($serviceName) . 'Service';
        }
        if ($mediaId == CommonEnum::OE_MEDIA) {
            $classname = '\app\service\analysis\\' . $serviceName . '\ocean\\' . ucfirst($dataType) . ucfirst($serviceName) . 'Service';
        }
        if ($mediaId == CommonEnum::QQ_MEDIA) {
            $classname = '\app\service\analysis\\' . $serviceName . '\qq\\' . ucfirst($dataType) . ucfirst($serviceName) . 'Service';
        }
        if ($classname) {
            return new $classname();
        }
        return null;
    }

    /**
     * 获取数据类型.
     *
     * @param int $type 类型
     */
    public static function getDataType(int $type = 0): string
    {
        if ($type == AnalysisEnum::TASK_TYPE_ACCOUNT) {
            return AnalysisEnum::TYPE_ACCOUNT;
        }
        if ($type == AnalysisEnum::TASK_TYPE_VIDEO) {
            return AnalysisEnum::TYPE_VIDEO;
        }
        if ($type == AnalysisEnum::TASK_TYPE_PICTURE) {
            return AnalysisEnum::TYPE_PIC;
        }

        return '';
    }

    /**
     * 媒体表前缀
     *
     * @param int $mediaId 媒体id
     */
    public static function getTablePrefix(int $mediaId = 0): string
    {
        if ($mediaId == CommonEnum::KS_MEDIA) {
            return AnalysisEnum::KS_TABLE_PREFIX;
        }
        if ($mediaId == CommonEnum::OE_MEDIA) {
            return AnalysisEnum::OE_TABLE_PREFIX;
        }
        if ($mediaId == CommonEnum::QQ_MEDIA) {
            return AnalysisEnum::QQ_TABLE_PREFIX;
        }
        return '';
    }

    /**
     * 获取素材日汇总表模型.
     * @param int $type 类型
     */
    public static function getMaterialDayStatModel(int $type = 0): MongoReportModel
    {
        $tableName = self::getDataType($type);
        $classname = '\app\model\mongo\MongoMaterial' . ucfirst($tableName) . 'DayStatModel';
        return new $classname();
    }
}
